/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.db.SqlManager;
import com.exception.DataException;
import com.javabeans.Menu;
import com.javabeans.Opcion;
import com.javabeans.SubMenu;
/**
 *
 * @author Administrador
 */
public class AdminOpcion implements OpcionManager{
    private Connection c;

    public AdminOpcion(Connection c){
        this.c = c;
    }
    
    /*==============================================================*/
    /* LISTA DE OPCIONES                                            */
    /*==============================================================*/
    @Override
    public List<Opcion> getOpciones() throws DataException{
        List<Opcion> opciones = new ArrayList<Opcion>();
        ResultSet rs = null;
        PreparedStatement preSt = null;
        String query = "";
        try{
            query = "SELECT O.IDMENU, O.IDSUBMENU, O.IDOPCION";
            query += ", O.NOMBRE, S.NOMBRE, M.NOMBRE";
            query += ", O.URL, S.URL";
            query += " FROM OPCION O";
            query += " INNER JOIN SUBMENU S ON O.IDMENU=S.IDMENU AND O.IDSUBMENU=S.IDSUBMENU";
            query += " INNER JOIN MENU M ON O.IDMENU=M.IDMENU";
            query += " ORDER BY M.ORDEN, S.ORDEN, O.ORDEN";            
            preSt = c.prepareStatement(query);
            rs = preSt.executeQuery();
            while(rs.next()){
                Opcion o = new Opcion();
                o.setIdMenu(rs.getInt("O.IDMENU"));
                o.setIdSubMenu(rs.getInt("O.IDSUBMENU"));
                o.setIdOpcion(rs.getInt("O.IDOPCION"));
                o.setNombre(rs.getString("O.NOMBRE"));
                o.setUrl(rs.getString("O.URL"));
                
                SubMenu s = new SubMenu();
                s.setIdMenu(o.getIdMenu());
                s.setIdSubMenu(o.getIdSubMenu());
                s.setNombre(rs.getString("S.NOMBRE"));
                s.setUrl(rs.getString("S.URL"));
                
                Menu m = new Menu();
                m.setIdMenu(s.getIdMenu());
                m.setNombre(rs.getString("M.NOMBRE"));
                
                o.setSubMenu(s);
                o.setMenu(m);
                
                opciones.add(o);
            }
        }catch(SQLException e){
            throw new DataException(" AdminOpcion -> getOpciones() e = "+e.getMessage());
        }finally{
            SqlManager.closeRs(rs);
            SqlManager.closePreSt(preSt);
        }
        
        return opciones;
    }
}
